package com.tianhui.consignortwofei.mvp.presenter;

import android.content.Context;

import com.fgs.common.CommonResponse;
import com.fgs.common.entity.CommonDataListInfo;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.tianhui.consignortwofei.manager.EventBusManage;
import com.tianhui.consignortwofei.mvp.contract.VehicleContract;
import com.tianhui.consignortwofei.mvp.model.VehicleModel;
import com.tianhui.consignortwofei.mvp.model.enty.VehicleInfo;

import java.util.List;
import java.util.Map;

/**
 * @author fgs
 * @date 2020/3/27
 **/
public class VehiclePresenter extends VehicleContract.Presenter {

    private final VehicleModel vehicleModel;
    private final Context mContext;

    public VehiclePresenter(Context context) {
        mContext = context;
        vehicleModel = new VehicleModel();
    }


    @Override
    public void requestVehicleList(Map<String, String> map, boolean isDialog) {
        vehicleModel.vehicleListModel(mContext, map, isDialog, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse<CommonDataListInfo<VehicleInfo>>>() {
            @Override
            public void onNext(CommonResponse<CommonDataListInfo<VehicleInfo>> commonResponse) {
                if (getView() != null) {
                    if (commonResponse.getCode() == 20000) {

                        CommonDataListInfo<VehicleInfo> commonDataListInfo = commonResponse.getData();
                        if (commonDataListInfo != null) {
                            getView().setPageInfo(commonDataListInfo.current, commonDataListInfo.pages);
                            List<VehicleInfo> list = commonDataListInfo.records;
                            if (list != null && list.size() > 0) {
                                getView().showDataList(list);
                            } else {
                                getView().showEmptyView();
                            }
                        }

                    } else {
                        getView().setMsg(commonResponse.getMsg());
                    }
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                if (getView() != null) {
                    getView().setMsg(e.message);
                }
            }
        });
    }

    @Override
    public void requestChangeVehicleStatus(Map<String, String> map, boolean isDialog) {
        vehicleModel.vehicleStatusModel(mContext, map, isDialog, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (getView() != null) {
                    if (commonResponse.getCode() == 20000) {
                        getView().setMsg(commonResponse.getMsg());
                        getView().refreshVehicleStatus();
                    } else {
                        getView().setMsg(commonResponse.getMsg());
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                if (getView() != null) {
                    getView().setMsg(e.message);
                }
            }
        });
    }

    @Override
    public void requestCreateStatement(Map<String, String> map, boolean isDialog) {
        vehicleModel.createStatementModel(mContext, map, isDialog, getView().bindLifecycle(), new ObserverResponseListener<CommonResponse>() {
            @Override
            public void onNext(CommonResponse commonResponse) {
                if (getView() != null) {
                    if (commonResponse.getCode() == 20000) {
                        getView().setMsg(commonResponse.getMsg());
                        EventBusManage.refreshVehicleList();
                    } else {
                        getView().setMsg(commonResponse.getMsg());
                    }
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                if (getView() != null) {
                    getView().setMsg(e.message);
                }
            }
        });
    }
}
